#include <iostream>
#include <cstring>
using namespace std;

class my_string
{
	friend ostream& operator<<(ostream& out, const my_string&);
	public:
		my_string(const char* str = NULL);
		my_string(const my_string&);
		my_string& operator=(const my_string&);
		my_string substr(size_t pose, size_t length);
		size_t find(const my_string&);
		~my_string(){
			delete[] this->m_ptr;
			this->m_ptr = NULL;
		}
	private:
		char* m_ptr;
};

my_string::my_string(const char* str)
{
	if(str == NULL)
	{
		m_ptr = new char[1];
		m_ptr[0] = '\0';
	}
	else
	{
		int length =  strlen(str);
		this->m_ptr = new(nothrow) char[length + 1];
		strcpy(this->m_ptr, str);
	}
}

my_string::my_string(const my_string& other)
{
	int length = strlen(other.m_ptr);
	this->m_ptr = new(nothrow) char[length + 1];
	strcpy(this->m_ptr, other.m_ptr);
}

my_string& my_string::operator=(const my_string& other)
{
	if(this == &other)
		return *this;
	delete[] this->m_ptr;
	int length = strlen(other.m_ptr);
	this->m_ptr = new(nothrow) char[length + 1];
	strcpy(this->m_ptr, other.m_ptr);
	return *this;
}

my_string my_string::substr(size_t pose, size_t length)
{
	size_t size = strlen(this->m_ptr);
	if(pose >= size)
		return my_string();
	char array[length + 1] = {0};
	char *temp = array;
	char *ptr = &this->m_ptr[pose];
	while(length-- && (*temp++ = *ptr++));
	return my_string(array);
}

size_t my_string::find(const my_string& other)
{
	char *temp = other.m_ptr;
	char *dest = this->m_ptr;
	size_t pose = -1;

	while(*dest)
	{
		char * find = dest;
		while(*find == *temp && *temp != '\0')
		{
			find++;
			temp++;
		}
		if(*temp == '\0')
		{
			pose = dest - this->m_ptr;
			break;
		}
		dest++;
		temp = other.m_ptr;
	}
	return pose;
}

ostream& operator<<(ostream& out, const my_string& other)
{
	out<<other.m_ptr;
	return out;
}

int main(void)
{
	my_string mystring("123434567890");
	cout<<mystring<<endl;
	my_string str1 = mystring.substr(2, 3);
	my_string str2("345");
	my_string str3 = str2;
	cout<<mystring.find(str2)<<endl;
	cout<<str2<<endl;
	return 0;
}
